<?php
class Current_User {

	private static $user;

	private function __construct() {}

	public static function user() {
		if(!isset(self::$user)) {
				
			$CI =& get_instance();
			//$CI->load->library('session');
			
			log_message('debug', 'checking CI userdata -> '.$CI->session->userdata('id'));
				
			if (!$id = $CI->session->userdata('id')) {
				return FALSE;
			}
				
			if (!$u = Doctrine::getTable('PersonModel')->find($id)) {
				return FALSE;
			}
				
			self::$user = $u;
		}

		return self::$user;
	}

	public static function getRoles() {
        return get_instance()->session->userdata('roles');
	}

	public static function login($id, $password) {
		log_message('debug', "login request with [$id,$password]");
		
		// get User object by username
		if ($u = Doctrine::getTable('PersonModel')->findOneById($id)) {
			log_message('debug', "found person in database -> ".$u->name);

			// to ge the mutated version of the input password
			$u_input = new PersonModel();
			$u_input->password = $password;
				
			// password match
			if ($u->password == $u_input->password) {
				unset($u_input);

				$CI =& get_instance();
				//$CI->load->library('session');
				$CI->session->set_userdata('id',$u->id);
				$CI->session->set_userdata('roles',PersonModel::getRolesArray($u->id));
				
				log_message('debug', "set userdata id to -> ".$CI->session->userdata('id'));
				
				self::$user = $u;

				return TRUE;
			}
				
			log_message('debug', "passwords don't match, unsetting self");
			unset($u_input);
		}
		
		log_message('debug', "login failed for ".$id);

		// login failed
		return FALSE;

	}

    public static function loginByEmail($email, $password) {
        log_message('debug', "login request with [$email,$password]");
        
        // get User object by username
        if ($u = Doctrine::getTable('PersonModel')->findOneByEmailPrimary($email)) {
            log_message('debug', "found person in database -> ".$u->name);

            // to ge the mutated version of the input password
            $u_input = new PersonModel();
            $u_input->password = $password;
                
            // password match
            if ($u->password == $u_input->password) {
                unset($u_input);

                $CI =& get_instance();
                //$CI->load->library('session');
                $CI->session->set_userdata('id',$u->id);
                $CI->session->set_userdata('roles',PersonModel::getRolesArray($u->id));
                
                log_message('debug', "set userdata id to -> ".$CI->session->userdata('id'));
                
                self::$user = $u;

                return TRUE;
            }
                
            log_message('debug', "passwords don't match, unsetting self");
            unset($u_input);
        }
        
        log_message('debug', "login failed for ".$email);

        // login failed
        return FALSE;
    }
	
	public static function logout() {
		if(isset(self::$user)) {
		  self::$user = null;
		}
	}

	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}

}
